package com.yst.metho.web.application.reports;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.TooManyListenersException;

import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.RadioButton;
import nextapp.echo2.app.TextField;
import nextapp.echo2.app.button.ButtonGroup;
import nextapp.echo2.app.filetransfer.UploadEvent;
import nextapp.echo2.app.filetransfer.UploadListener;
import nextapp.echo2.app.filetransfer.UploadSelect;

import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.suivi.data.reports.Report;
import com.yst.suivi.data.reports.ReportBean;
import com.yst.suivi.util.Repository;

/**
 * Vue qui permet de creer un rapport.
 * 
 * @author perso
 * 
 */
public class ReportPane extends Column
{
	public static final String FILE_SEPARATOR = ", ";

	private TextField mNom;

	private Label mPath;

	private boolean mIsFileTransfered = false;

	private String mInnerFileName;

	private ButtonGroup mTypeGroup;

	public ReportPane(Report report)
	{
		super();
		setCellSpacing(new Extent(10));
		setInsets(new Insets(10));
		Grid headerGrid = new Grid();
		headerGrid.setInsets(new Insets(0, 2));
		add(headerGrid);
		Label label;
		label = new Label("Nom : ");
		headerGrid.add(label);

		mNom = new TextField();
		headerGrid.add(mNom);

		mTypeGroup = new ButtonGroup();
		RadioButton btn = new RadioButton("Global");
		btn.setGroup(mTypeGroup);
		btn.setSelected(true);
		btn.setProperty("TYPE", Integer.toString(ReportBean.GLOBAL_REPORT));
		headerGrid.add(btn);
		btn = new RadioButton("Projet");
		btn.setGroup(mTypeGroup);
		btn.setProperty("TYPE", Integer.toString(ReportBean.PROJECT_REPORT));
		headerGrid.add(btn);

		label = new Label("Fichier : ");
		headerGrid.add(label);
		mPath = new Label("");
		headerGrid.add(mPath);

		label = new Label("");
		headerGrid.add(label);

		UploadSelect uploadSelect = new UploadSelect();
		uploadSelect.setSendButtonDisplayed(false);
		headerGrid.add(uploadSelect);
		try
		{
			uploadSelect.addUploadListener(new UploadListener()
			{
				public void fileUpload(UploadEvent uploadEvent)
				{
					// Read out the content of the uploaded file.
					try
					{
						File fichier = new File(Repository.getRealPath(uploadEvent.getFileName()));
						// fichier = File.createTempFile("suiviReport_",
						// ".jrxml", fichier);
						if (!fichier.exists())
						{
							fichier.createNewFile();
						}
						ReportPane.copyToFile(uploadEvent.getInputStream(), fichier);
						mPath.setText(mPath.getText() + uploadEvent.getFileName() + FILE_SEPARATOR);
						mIsFileTransfered = true;
						mInnerFileName = mPath.getText();
					}
					catch (IOException e)
					{
						ExceptionMgt.manage(e);
					}
				}

				public void invalidFileUpload(UploadEvent uploadEvent)
				{
					ExceptionMgt.displayMessageBox("Error during file transfert.");
				}
			});
		}
		catch (TooManyListenersException e)
		{
			ExceptionMgt.manage(e);
		}

		if (null != report)
		{
			mNom.setText(report.getNom());
		}
	}

	public String getNom()
	{
		return mNom.getText();
	}

	/**
	 * @return the path
	 */
	public String getPath()
	{
		return mPath.getText();
	}

	/**
	 * @return the isFileTransfered
	 */
	public boolean isFileTransfered()
	{
		return mIsFileTransfered;
	}

	/**
	 * @return the innerFileName
	 */
	public String getInnerFileName()
	{
		return mInnerFileName;
	}

	/**
	 * Return the type of the project.
	 * @see ReportBean
	 * 
	 * @return
	 */
	public int getType()
	{
		int retour = ReportBean.GLOBAL_REPORT;

		for (int i = 0; i < mTypeGroup.getButtons().length; i++)
		{
			if (mTypeGroup.getButtons()[i].isSelected())
			{
				retour = Integer.parseInt(mTypeGroup.getButtons()[i].getProperty("TYPE").toString());
			}
		}
		return retour;
	}

	public static void copyToFile(File fichierSource, File fichierDestination) throws FileNotFoundException, IOException
	{
		copyToFile(new FileInputStream(fichierSource), fichierDestination);
	}

	public static void copyToFile(InputStream stream, File fichierDestination) throws FileNotFoundException, IOException
	{
		FileOutputStream fos = new FileOutputStream(fichierDestination);
		byte[] buffer = new byte[1024];
		int countByte = 0;
		do
		{
			countByte = stream.read(buffer);
			// ecriture des donnees s'il y a eu des infos lus.
			if (-1 != countByte)
			{
				fos.write(buffer, 0, countByte);
			}
		}
		while (-1 != countByte);
		fos.close();
	}
}
